//
//  TwitterClient3ViewController.m
//  TwitterClient3
//
//  Created by goccy on 11/10/28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TwitterClient3ViewController.h"

@implementation TwitterClient3ViewController
@synthesize timelineController;
-(void)initializeTimeLine
{
	timeline.delegate = timelineController;
	timeline.dataSource = timelineController;
}

-(void)setTimeLineController:(TimeLine *)timelineController_
{
	timelineController = timelineController_;
}

-(TimeLine *)getTimeLineController
{
	return timelineController;
}

-(IBAction)clickedTweetButton:(id)sender
{
	NSString *tweet_text = tweetBox.text;
	tweetBox.text = @"";
	[tweetBox resignFirstResponder];
	char request_url[128] = {0};
	snprintf(request_url, 128, "http://%s/cgi-bin/twitter/tweet.json", HOST_NAME);
	NSString* str = [NSString stringWithCString: request_url encoding:NSUTF8StringEncoding];
	NSURL *url = [NSURL URLWithString:str];
	NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
	[urlRequest setTimeoutInterval:1.0];
	[urlRequest setHTTPMethod:@"POST"];
	[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
	NSString *post_body = [NSString stringWithFormat:@"text=%@&username=%@", tweet_text, @"goccy"];
	NSLog(@"%@", post_body);
	NSData *requestData = [post_body dataUsingEncoding:NSUTF8StringEncoding];
	//NSData *requestData = [post_body dataUsingEncoding:NSShiftJISStringEncoding];
	[urlRequest setHTTPBody:requestData];
	NSURLResponse *response;
	NSError *error = nil;
	NSData *result = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
	if (!result || error) {
		[alertView show];
		NSLog(@"error = %@", error);
		return;
	}
	NSString *resultString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
	NSLog(@"result == %@", resultString);
	[timelineController requestTakeTimeline];
	[resultString release];
	[urlRequest release];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	UITableView *myTableView = [[UITableView alloc] initWithFrame:[self.view bounds]];
	CGRect rect = CGRectMake(0, 247, 775, 793);
	myTableView.frame = rect;
	myTableView.delegate = timelineController;
	myTableView.dataSource = timelineController;
	timelineController.tableView = myTableView;
	[self.view addSubview:myTableView];
	userImage.image = [UIImage imageNamed:@"grav_firebird_birdy.jpg"];
	alertView = [[UIAlertView alloc]
				 initWithTitle:@"Connection Error"
				 message:@"Sorry, please request a few minitues later" delegate:nil
				 cancelButtonTitle:@"close"
				 otherButtonTitles:nil];
	[self.view addSubview:alertView];
    [super viewDidLoad];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
